﻿Imports System.Reflection
Imports System.Windows.Forms
Imports mshtml
'
Module CommonModule

    Public 是否从窗口调用英语测试程序 As Boolean = False
    Public 是否从窗口调用英语浏览程序 As Boolean = False
    Public 是否为按浏览次数 As Boolean = False
    Public 当前页数 As Integer = 0
    Public 当前英语备注ID As Integer = 0
    Public 当前英语备注内容 As String = ""

    Public ct As New Wys.Db.Vb.CTable

#Region "增加在标题栏显示当前的id 修改时间  A-2010-01-28"
    Public Sub RefreshTitle(ByVal su As System.Windows.Forms.StatusStrip, ByVal strAuthor As String, ByVal strAddTime As String, ByVal strTime As String, ByVal strId As String)
        If su IsNot Nothing Then
            su.Items(0).Text = "ID:" + strId.ToString() + " 添加时间:" + strAddTime + " 最后修改时间:" + strTime.ToString() + " 创建者:" + strAuthor
        End If
    End Sub

    Public Sub RefreshTitle(ByVal su As System.Windows.Forms.StatusStrip, ByVal strContent As String)
        If su IsNot Nothing Then
            su.Items(0).Text = strContent
        End If
    End Sub

    Public Sub RefreshEnglishMemo(ByVal su As System.Windows.Forms.StatusStrip)
        If 当前英语备注ID > 0 Then
            CommonModule.RefreshTitle(su, "备注ID: " + CommonModule.当前英语备注ID.ToString + " 备注内容: " + CommonModule.当前英语备注内容)
        Else
            CommonModule.RefreshTitle(su, " 没有设置备注！没有设置备注！")
        End If
    End Sub

    Public Sub RefreshEnglishTitle(ByVal su As System.Windows.Forms.StatusStrip, ByVal strAuthor As String, ByVal strAddTime As String, ByVal strTime As String, ByVal strId As String)
        If su IsNot Nothing Then
            If 当前英语备注ID > 0 Then
                su.Items(0).Text = "ID:" + strId.ToString() + " 添加时间:" + strAddTime + " 最后修改时间:" + strTime.ToString()
            Else
                su.Items(0).Text = "ID:" + strId.ToString() + " 添加时间:" + strAddTime + " 最后修改时间:" + strTime.ToString() + " 创建者为：" + strAuthor
            End If
        End If

        If 当前英语备注ID > 0 Then
            su.Items(0).Text &= " 备注ID:" + CommonModule.当前英语备注ID.ToString + " 备注内容:" + CommonModule.当前英语备注内容
        Else
            su.Items(0).Text &= " 没有设置备注！"
        End If
    End Sub
#End Region

#Region "保存图片"
    Public Sub SaveImage(ByVal strCurrentDirectory As String, _
                         ByRef HtmlEditor1 As Wys.Control.CSharp.HtmlEditor, _
                         ByVal dataTableName As String, _
                         ByVal RecordId As String, _
                         ByRef frm As Wys.Control.Vb.AutoAdjustFrom)
        'A-2009-12-14 查找图片 下载图片
        '删除文件 A-2010-03-27
        If ct Is Nothing Then
            ct = New Wys.Db.Vb.CTable()
        End If
        '删除文件 A-2010-03-27
        Dim strTitle As String = frm.Text
        Dim htmes As System.Windows.Forms.HtmlElementCollection = HtmlEditor1.GetImages()
        Dim strCurrent As String = strCurrentDirectory
        Dim strTemp As String = ""
        Dim strTemp1 As String = ""
        Dim strDir As String = ""
        Dim wc As New System.Net.WebClient
        Dim count As Integer = 0
        'A-2010-1-2
        Dim lstSource As New List(Of String)
        Dim lstTarget As New List(Of String)
        'A-2010-1-2
        For Each element As System.Windows.Forms.HtmlElement In htmes
            Try
                count = count + 1
                strDir = System.IO.Path.GetDirectoryName(element.GetAttribute("src"))
                strTemp1 = System.IO.Path.Combine(strCurrentDirectory, "Images")
                If strDir <> strTemp1 Then
                    'strTemp = "Images\" & System.Guid.NewGuid.ToString("N") & System.IO.Path.GetExtension(element.GetAttribute("src"))
                    'dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & 
                    strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(element.GetAttribute("src")))
                    'A-2010-11-10 修正连接地址为
                    '链接地址为http://tnrltw.blu.livefilestore.com/y1pJhyXxD6s43uLuZaPDX2RKLjN1Tb1qjUofRGey02pZp8OdE_ku5i_ZsuW0WQJaCRMVIgo6yV38b5vYptrnonvqw?PARTNER=WRITER
                    If UTIL.CUtil.IsImagePaath(System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(element.GetAttribute("src")))) Then
                        'A-2010-1-2
                        strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(element.GetAttribute("src")))
                        'A-2010-1-2
                    Else
                        'A-2010-11-10
                        Dim strGuid As String = System.Guid.NewGuid.ToString("D")
                        strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & strGuid + ".gif"
                        'A-2010-11-10
                    End If
                    'A-2010-11-10 修正连接地址为
                    Try
                        If lstSource.Contains(element.GetAttribute("src")) Then
                            Dim idex As Integer = 0
                            For Each Str As String In lstSource
                                If Str.ToLower = element.GetAttribute("src").ToLower Then
                                    Exit For
                                End If
                                idex = idex + 1
                            Next
                            System.Windows.Forms.Application.DoEvents()
                            element.SetAttribute("src", "#王永胜图片替换路径1234#" + lstTarget(idex))
                        Else
                            frm.Text = "开始下载文件:" + element.GetAttribute("src") + "....."
                            System.Windows.Forms.Application.DoEvents()
                            wc.DownloadFile(element.GetAttribute("src"), strTemp)
                            frm.Text = "开始下载文件:" + element.GetAttribute("src") + "成功"
                            System.Windows.Forms.Application.DoEvents()
                            Dim strPath23 As String = System.IO.Path.Combine(strCurrentDirectory, strTemp)
                            frm.Text = "获取" + element.GetAttribute("src") + "真实的扩展名....."
                            System.Windows.Forms.Application.DoEvents()
                            Dim strTarget As String = UTIL.CUtil.GetReallyImage(strPath23, strTemp)
                            System.Windows.Forms.Application.DoEvents()
                            '图片的格式不正确
                            If strTarget = "" Then
                                '保留源连接
                                frm.Text = "获取" + strPath23 + "真实的扩展名失败,开始删除"
                                System.Windows.Forms.Application.DoEvents()
                                Try
                                    System.IO.File.Delete(strPath23)
                                    '删除文件 A-2010-03-27
                                    ct.AddDeleteDetalImageUpload(System.IO.Path.GetFileName(strPath23), frm.strName)
                                    '删除文件 A-2010-03-27
                                Catch ex As Exception
                                    UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
                                End Try
                            Else
                                'A-2010-1-2
                                If Not lstSource.Contains(element.GetAttribute("src")) Then
                                    lstSource.Add(element.GetAttribute("src"))
                                    lstTarget.Add(strTemp)
                                End If
                                'A-2010-1-2
                                frm.Text = "获取" + strPath23 + "真实的扩展名成功,开始替换"
                                System.Windows.Forms.Application.DoEvents()
                                element.SetAttribute("src", "#王永胜图片替换路径1234#" + strTemp)
                                System.Windows.Forms.Application.DoEvents()
                            End If
                        End If
                    Catch ex As Exception
                        UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
                    End Try
                End If
            Catch ex As Exception
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
            End Try
            System.Windows.Forms.Application.DoEvents()
            frm.Text = strTitle
        Next
        'A-2009-12-14
    End Sub
#End Region

#Region "替换图片 路径"
    '替换图片 路径
    Public Function RepalceImageUrl(ByVal strCurrentDirectory As String, _
                                    ByVal strSource As String) As String
        Return strSource.Replace("#王永胜图片替换路径1234#", strCurrentDirectory)
    End Function
#End Region

#Region "删除文件"
    '删除文件
    Public Sub DeleteFile(ByVal strCurrentDirectory As String, _
                          ByRef HtmlEditor1 As Wys.Control.CSharp.HtmlEditor, _
                          ByVal dataTableName As String, _
                          ByVal RecordId As String, _
                          ByRef frm As Wys.Control.Vb.AutoAdjustFrom)
        '删除文件 A-2010-03-27
        If ct Is Nothing Then
            ct = New Wys.Db.Vb.CTable()
        End If
        '删除文件 A-2010-03-27
        'A-2009-12-14 查找图片删除图片
        Dim strTitle As String = frm.Text
        Dim htmes As System.Windows.Forms.HtmlElementCollection = HtmlEditor1.GetImages()
        Dim strCurrent As String = strCurrentDirectory
        Dim strTemp As String = ""
        Dim strTemp1 As String = ""
        Dim strDir As String = ""
        Dim wc As New System.Net.WebClient
        'For Each element As System.Windows.Forms.HtmlElement In htmes
        '    strDir = System.IO.Path.GetDirectoryName(New Uri(element.GetAttribute("src")).AbsolutePath)
        '    strTemp1 = System.IO.Path.Combine(strCurrentDirectory, "Images")
        '    If strDir = strTemp1 Then
        '        Try
        '            System.IO.File.Delete(New Uri(element.GetAttribute("src")).AbsolutePath)
        '        Catch ex As Exception
        '            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
        '        End Try
        '    End If
        'Next
        '获取图片文件夹得路径
        strTemp1 = System.IO.Path.Combine(strCurrentDirectory, "Images")
        Dim dirs As New IO.DirectoryInfo(strTemp1)
        If Not IO.Directory.Exists(strTemp1) Then
            Exit Sub
        End If
        If dirs IsNot Nothing Then
            '获取所有的文件
            Dim fins() As IO.FileInfo = dirs.GetFiles()
            For Each fin As IO.FileInfo In fins
                '查找此记录的图片
                If fin.Name.ToLower().StartsWith((dataTableName & "_" & RecordId).ToLower()) Then
                    Try
                        frm.Text = "开始删除" + fin.FullName + "......"
                        '删除文件 A-2010-03-27
                        ct.AddDeleteDetalImageUpload(fin.Name, frm.strName)
                        '删除文件 A-2010-03-27
                        '删除
                        fin.Delete()
                        frm.Text = "删除" + fin.FullName + "成功"
                    Catch ex As Exception
                        UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
                    End Try
                End If
            Next
        End If
        frm.Text = strTitle
        'A-2009-12-14
    End Sub
#End Region

#Region "更新文件"
    '更新文件
    Public Sub ModifyFile(ByVal strCurrentDirectory As String, _
                          ByRef HtmlEditor1 As Wys.Control.CSharp.HtmlEditor, _
                          ByVal dataTableName As String, _
                          ByVal RecordId As String, _
                          ByRef frm As Wys.Control.Vb.AutoAdjustFrom)
        '删除文件 A-2010-03-27
        If ct Is Nothing Then
            ct = New Wys.Db.Vb.CTable()
        End If
        '删除文件 A-2010-03-27
        'A-2009-12-14 查找图片删除图片
        Dim strTitle As String = frm.Text
        Dim htmes As System.Windows.Forms.HtmlElementCollection = HtmlEditor1.GetImages()
        Dim strCurrent As String = strCurrentDirectory
        Dim strTemp As String = ""
        Dim strTemp1 As String = ""
        Dim strDir As String = ""
        Dim wc As New System.Net.WebClient
        Dim count As Integer = 0
        'A-2010-1-2
        Dim lstSource As New List(Of String)
        Dim lstTarget As New List(Of String)
        'A-2010-1-2
        For Each element As System.Windows.Forms.HtmlElement In htmes
            strDir = System.IO.Path.GetDirectoryName(New Uri(element.GetAttribute("src")).AbsolutePath)
            strTemp1 = System.IO.Path.Combine(strCurrentDirectory, "Images")
            If strDir <> strTemp1 Then
                Try
                    Dim strAb As String = New Uri(element.GetAttribute("src")).AbsolutePath
                    strDir = System.IO.Path.GetDirectoryName(element.GetAttribute("src"))
                    strTemp1 = System.IO.Path.Combine(strCurrentDirectory, "Images")
                    If strDir <> strTemp1 Then
                        'D-2010-1-2
                        'strTemp = System.IO.Path.Combine(strCurrent, "Images\" & System.Guid.NewGuid.ToString("N") & System.IO.Path.GetExtension(element.GetAttribute("src")))
                        'A-2010-11-10 修正连接地址为
                        '链接地址为http://tnrltw.blu.livefilestore.com/y1pJhyXxD6s43uLuZaPDX2RKLjN1Tb1qjUofRGey02pZp8OdE_ku5i_ZsuW0WQJaCRMVIgo6yV38b5vYptrnonvqw?PARTNER=WRITER
                        If UTIL.CUtil.IsImagePaath(System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(element.GetAttribute("src")))) Then
                            'A-2010-1-2
                            strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(element.GetAttribute("src")))
                            While System.IO.File.Exists(strTemp)
                                count = count + 1
                                strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(element.GetAttribute("src")))
                            End While
                            strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(element.GetAttribute("src")))
                            'A-2010-1-2
                        Else
                            'A-2010-11-10
                            Dim strGuid As String = System.Guid.NewGuid.ToString("D")
                            strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & strGuid + ".gif"
                            While System.IO.File.Exists(strTemp)
                                count = count + 1
                                strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & strGuid + ".gif"
                            End While
                            strTemp = "Images\" & dataTableName & "_" & RecordId & "_" + count.ToString("00") & "_" & strGuid + ".gif"
                            'A-2010-11-10
                        End If
                        'A-2010-11-10 修正连接地址为
                        Try
                            If lstSource.Contains(element.GetAttribute("src")) Then
                                Dim idex As Integer = 0
                                For Each Str As String In lstSource
                                    If Str.ToLower = element.GetAttribute("src").ToLower Then
                                        Exit For
                                    End If
                                    idex = idex + 1
                                Next
                                System.Windows.Forms.Application.DoEvents()
                                element.SetAttribute("src", "#王永胜图片替换路径1234#" + lstTarget(idex))
                            Else
                                frm.Text = "开始下载文件:" + element.GetAttribute("src") + "....."
                                System.Windows.Forms.Application.DoEvents()
                                wc.DownloadFile(element.GetAttribute("src"), strTemp)
                                frm.Text = "下载文件:" + element.GetAttribute("src") + "成功"
                                System.Windows.Forms.Application.DoEvents()
                                'element.SetAttribute("src", strTemp)
                                Dim strPath23 As String = System.IO.Path.Combine(strCurrentDirectory, strTemp)
                                frm.Text = "获取" + element.GetAttribute("src") + "真实的扩展名....."
                                System.Windows.Forms.Application.DoEvents()
                                Dim strTarget As String = UTIL.CUtil.GetReallyImage(strPath23, strTemp)
                                '图片的格式不正确
                                If strTarget = "" Then
                                    '保留源连接
                                    frm.Text = "获取" + strPath23 + "真实的扩展名失败,开始删除"
                                    System.Windows.Forms.Application.DoEvents()
                                    Try
                                        System.IO.File.Delete(strPath23)
                                        '删除文件 A-2010-03-27
                                        ct.AddDeleteDetalImageUpload(System.IO.Path.GetFileName(strPath23), frm.strName)
                                        '删除文件 A-2010-03-27
                                    Catch ex As Exception
                                        UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
                                    End Try
                                Else
                                    'A-2010-1-2
                                    If Not lstSource.Contains(element.GetAttribute("src")) Then
                                        lstSource.Add(element.GetAttribute("src"))
                                        lstTarget.Add(strTemp)
                                    End If
                                    'A-2010-1-2
                                    frm.Text = "获取" + strPath23 + "真实的扩展名成功,开始替换"
                                    System.Windows.Forms.Application.DoEvents()
                                    element.SetAttribute("src", "#王永胜图片替换路径1234#" + strTemp)
                                End If
                            End If
                        Catch ex As Exception
                            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
                        End Try
                    End If
                Catch ex As Exception
                    UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
                End Try
            Else
                strTemp = System.Web.HttpUtility.UrlDecode(System.IO.Path.GetFileName(New Uri(element.GetAttribute("src")).AbsolutePath))
                element.SetAttribute("src", "#王永胜图片替换路径1234#" + "Images\" + strTemp)
            End If
        Next
        frm.Text = strTitle
        'A-2009-12-14
    End Sub
#End Region

#Region "下载文件"
    Public Function DownLoadFile(ByVal strCurrentDirectory As String, _
                                 ByRef HtmlEditor1 As Wys.Control.CSharp.HtmlEditor, _
                                 ByVal dataTableName As String, _
                                 ByVal RecordId As String, _
                                 ByRef frm As Wys.Control.Vb.AutoAdjustFrom) As String
        Dim res As String = ""
        Try
            Dim hcs As List(Of HtmlElement) = HtmlEditor1.GetDownLoadLinks
            For Each hc As HtmlElement In hcs

            Next
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod().GetType())
            GoTo Function_End
        End Try
Function_End:
        Return res
    End Function
#End Region

#Region "获取当前浏览器控件里的元素的CSS 返回一个列表 A-2010-05-26"
    Public Function GetCss(ByVal strCurrentDirectory As String, _
                      ByRef HtmlEditor1 As Wys.Control.CSharp.HtmlEditor, _
                      ByVal dataTableName As String, _
                      ByVal RecordId As String, _
                      ByRef frm As Wys.Control.Vb.AutoAdjustFrom, _
                      ByRef url As String) As String
        GetCss = ""
        If url.Trim = "" Then
            Exit Function
        End If
        Dim strCaption As String = frm.Text
        Dim tbTable As New DataTable
        tbTable.Columns.Add("CSS名称")
        tbTable.Columns.Add("内容")
        Using wb As WebBrowser = New WebBrowser
            wb.Navigate(url)
            Dim iCount As Integer = 0
            While wb.ReadyState <> WebBrowserReadyState.Complete
                frm.Text = "正在读取关联网址内容......"
                System.Windows.Forms.Application.DoEvents()
                System.Threading.Thread.Sleep(200)
                iCount += 1
                If iCount > 25 Then
                    Exit While
                End If
            End While
            '开始查询CSS            
            Dim hd As mshtml.HTMLDocument = wb.Document.DomDocument
            If hd IsNot Nothing AndAlso hd.styleSheets.length Then
                For idex As Integer = 0 To hd.styleSheets.length - 1
                    Dim hsc As IHTMLStyleSheet = hd.styleSheets.item(idex)
                    If hsc IsNot Nothing AndAlso hsc.rules.length > 0 Then
                        For jdex As Integer = 0 To hsc.rules.length - 1
                            Dim dr As DataRow = tbTable.NewRow
                            dr("CSS名称") = hsc.rules.item(jdex).selectorText
                            dr("内容") = hsc.rules.item(jdex).style.cssText
                            tbTable.Rows.Add(dr)
                        Next
                    End If
                Next
            End If
        End Using
        frm.Text = "开始分析......"
        System.Windows.Forms.Application.DoEvents()
        '如果CSS表中存在数据
        If tbTable.Rows.Count = 0 Then
            Exit Function
        End If
        ''获取实际需要的CSS###################################################################################################
        Dim dtNewCss As DataTable = tbTable.Clone

        For Each dr As DataRow In tbTable.Rows
            Dim strClasss() As String = CStr(dr("CSS名称")).Trim.Split(New String() {",", " "}, StringSplitOptions.RemoveEmptyEntries)
            If strClasss.Length > 0 Then
                '表示的是Class 名称
                If CStr(dr("CSS名称")).Trim.StartsWith(".") Then
                    For Each he As HtmlElement In HtmlEditor1.webBrowserBody.Document.All
                        If he.GetAttribute("classname").ToUpper = strClasss(0).ToUpper Then
                            dtNewCss.ImportRow(dr)
                            Continue For
                        End If
                    Next
                    'ID #
                ElseIf CStr(dr("CSS名称")).Trim.StartsWith("#") Then
                    Dim he As HtmlElement = HtmlEditor1.webBrowserBody.Document.GetElementById(strClasss(0).Substring(0, strClasss(0).Length - 1))
                    If he IsNot Nothing Then
                        dtNewCss.ImportRow(dr)
                        Continue For
                    End If
                    'TAG 标记 如 P
                Else
                    Dim hes As System.Windows.Forms.HtmlElementCollection = HtmlEditor1.webBrowserBody.Document.GetElementsByTagName(strClasss(0))
                    If hes.Count > 0 Then
                        dtNewCss.ImportRow(dr)
                        Continue For
                    End If
                End If
            End If

        Next
        frm.Text = "分析结束......"
        System.Windows.Forms.Application.DoEvents()
        If dtNewCss.Rows.Count > 0 Then
            Dim sb As New StringBuilder
            For Each dr As DataRow In dtNewCss.Rows
                sb.AppendLine(dr("CSS名称") + "{" + dr("内容") + "}")
            Next
            Return sb.ToString()
        End If
        frm.Text = strCaption
        System.Windows.Forms.Application.DoEvents()
    End Function
#End Region

#Region "根据日期得到星期几"
    Public Function GetWeek(ByVal strDate) As String
        GetWeek = ""
        Try
            If Not IsDate(strDate) Then
                Exit Function
            End If
            Dim dt1 As DayOfWeek = DateTime.Parse(strDate).DayOfWeek
            If dt1 = DayOfWeek.Sunday Then
                GetWeek = "星期日"
            ElseIf dt1 = DayOfWeek.Friday Then
                GetWeek = "星期五"
            ElseIf dt1 = DayOfWeek.Saturday Then
                GetWeek = "星期六"
            ElseIf dt1 = DayOfWeek.Monday Then
                GetWeek = "星期一"
            ElseIf dt1 = DayOfWeek.Thursday Then
                GetWeek = "星期四"
            ElseIf dt1 = DayOfWeek.Tuesday Then
                GetWeek = "星期二"
            ElseIf dt1 = DayOfWeek.Wednesday Then
                GetWeek = "星期三"
            End If
            GetWeek = "(" + GetWeek + ")"
        Catch ex As Exception
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType())
        End Try

    End Function
#End Region

End Module
